package com.xudow.app.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.xudow.app.XApplication;
import com.xudow.app.model.SimpleUser;

import static com.xudow.app.util.LogUtils.makeLogTag;

/**
 * Utilities and constants related to app preferences.
 *
 * @author lukecheng
 */
public class PrefUtils {
    private static final String TAG = makeLogTag("PrefUtils");

    /**
     * 是否已经登录
     */
    public static final String PREF_LOGIN_STATUS = "pref_login_status";

    /**
     * 用户名
     */
    public static final String PREF_USERNAME = "pref_username";

    /**
     * 密码
     */
    public static final String PREF_PASSWORD = "pref_password";

    /**
     * 首页消息timestamp
     */
    public static final String PREF_HOME_MESSAGE_TIMESTAMP = "pref_home_message_timestamp";

    /**
     * 消息timestamp
     */
    public static final String PREF_MESSAGE_TIMESTAMP = "pref_message_timestamp";

    /**
     * 是否第一次登录
     */
    public static final String PREF_FIRST_LAUNCH = "pref_first_launch";

    /**
     * 是否开启推送
     */
    public static final String PREF_PUSH_ENABLE = "pref_push_enable";

    /**
     * 判断是否已登录
     *
     * @param context
     * @return
     */
    public static boolean isLoginBefore(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getBoolean(PREF_LOGIN_STATUS, false);
    }

    /**
     * 设置登陆pref
     *
     * @param context
     * @param status
     */
    public static void setPrefLoginStatus(final Context context, final boolean status) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        sp.edit().putBoolean(PREF_LOGIN_STATUS, status).commit();
    }

    /**
     * 保存用户信息
     *
     * @param context
     * @param user
     */
    public static void saveUserInfo(final Context context, SimpleUser user) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(PREF_USERNAME, user.getUsername());

        if (StringUtils.isEmpty(user.getPassword())) {
            editor.putString(PREF_PASSWORD, user.getPassword());
        } else {
            //TODO:加密用户密码
            editor.putString(PREF_PASSWORD, user.getPassword());
        }

        editor.commit();
    }

    /**
     * 获取用户信息
     *
     * @param context
     * @return
     */
    public static SimpleUser getUserInfo(final Context context) {
        SimpleUser user = new SimpleUser();

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        user.setUsername(sp.getString(PREF_USERNAME, ""));

        String password = sp.getString(PREF_PASSWORD, "");

        if (StringUtils.isEmpty(password)) {
            user.setPassword(password);
        } else {
            //TODO:解密密码
            user.setPassword(password);
        }

        return user;
    }

    /**
     * 保存首页消息timestamp
     *
     * @param context
     * @param timestamp
     */
    public static void saveHomeMessageTimeStamp(final Context context, String timestamp) {
        XApplication application = (XApplication) context.getApplicationContext();

        String prefKey = PREF_HOME_MESSAGE_TIMESTAMP;
        if (application.isLogin()) {
            prefKey += application.getUserProfileId();
        }

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(prefKey, timestamp);
        editor.commit();
    }

    /**
     * 获取首页消息timestamp
     *
     * @param context
     * @return
     */
    public static String getHomeMessageTimeStamp(final Context context) {
        if (context == null) {
            return "";
        }
        XApplication application = (XApplication) context.getApplicationContext();

        String prefKey = PREF_HOME_MESSAGE_TIMESTAMP;
        if (application.isLogin()) {
            prefKey += application.getUserProfileId();
        }

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getString(prefKey, "");
    }

    public static boolean isFirstLaunch(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        boolean value = sp.getBoolean(PREF_FIRST_LAUNCH, true);

        return value;
    }

    public static boolean isSelectGrade(Context context){
        SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences(context);
                String str=sp.getString("GradeIds","");
        return !str.equals("");
    }
    public static void removeFirstLaunch(final Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        sp.edit().putBoolean(PREF_FIRST_LAUNCH, false).commit();
    }

    /**
     * 保存消息timestamp
     *
     * @param context
     * @param timestamp
     */
    public static void saveMessageTimeStamp(final Context context, String timestamp) {
        XApplication application = (XApplication) context.getApplicationContext();

        String prefKey = PREF_MESSAGE_TIMESTAMP;
        if (application.isLogin()) {
            prefKey += application.getUserProfileId();
        }

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(prefKey, timestamp);
        editor.commit();
    }

    /**
     * 获取消息timestamp
     *
     * @param context
     * @return
     */
    public static String getMessageTimeStamp(final Context context) {
        XApplication application = (XApplication) context.getApplicationContext();

        String prefKey = PREF_MESSAGE_TIMESTAMP;
        if (application.isLogin()) {
            prefKey += application.getUserProfileId();
        }

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getString(prefKey, "");
    }

    /**
     * 判断是否开启推送
     *
     * @param context
     * @return
     */
    public static boolean isPushEnable(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getBoolean(PREF_PUSH_ENABLE, true);
    }

    /**
     * 设置登陆pref
     *
     * @param context
     * @param status
     */
    public static void setPushEnable(final Context context, final boolean status) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        sp.edit().putBoolean(PREF_PUSH_ENABLE, status).commit();
    }
}
